library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.2
setwd("/Users/kendraosburn/syracuse/719")
happiness <- read.csv('happiness_project.csv', sep=",", header=TRUE)
str(happiness)
## 'data.frame': 447 obs. of 13 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ country : Factor w/ 151 levels "Afghanistan",..: 129 57 36 101 25 43 95 128 96 7 ...
## $ region : Factor w/ 10 levels "Australia and New Zealand",..: 10 10 10 10 6 10 10 10 1 1 ...
## $ year : int 2015 2015 2015 2015 2015 2015 2015 2015 2015 2015 ...
## $ happiness_rank : int 1 2 3 4 5 6 7 8 9 10 ...
## $ happiness_score : num 7.59 7.56 7.53 7.52 7.43 ...
## $ economy_gdp_per_capita : num 1.4 1.3 1.33 1.46 1.33 ...
## $ family : num 1.35 1.4 1.36 1.33 1.32 ...
## $ health_life_expectancy : num 0.941 0.948 0.875 0.885 0.906 ...
## $ freedom : num 0.666 0.629 0.649 0.67 0.633 ...
## $ trust_government_corruption: num 0.42 0.141 0.484 0.365 0.33 ...
## $ generosity : num 0.297 0.436 0.341 0.347 0.458 ...
## $ dystopia_residual : num 2.52 2.7 2.49 2.47 2.45 ...
ggplot(happiness, aes(happiness_rank, family)) +
geom_point()
ggplot(happiness, aes(happiness_rank, health_life_expectancy)) +
geom_point()
ggplot(happiness, aes(happiness_rank, family, color = region)) +
geom_point()
# Turning year into a categorial variable
happiness$year <- as.factor(happiness$year)
ggplot(happiness, aes(happiness_rank, family, shape = year)) +
geom_point()
ggplot(happiness, aes(happiness_rank, family, size = economy_gdp_per_capita)) +
geom_point()
ggplot(happiness, aes(happiness_rank, health_life_expectancy)) +
geom_point(aes(color = "blue"))
ggplot(happiness, aes(happiness_rank, health_life_expectancy)) +
geom_point(color = "blue")
ggplot(happiness, aes(happiness_rank, health_life_expectancy)) +
geom_point() +
facet_wrap(~region)
ggplot(happiness, aes(happiness_rank, health_life_expectancy, shape = year)) +
geom_point() +
facet_wrap(~region)
ggplot(happiness, aes(happiness_rank, health_life_expectancy)) +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(happiness, aes(happiness_rank, health_life_expectancy)) +
geom_point() +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(happiness, aes(year, happiness_score)) +
geom_boxplot()
FUN <- function(dat, x, y) {
ggplot(dat, aes_string(x = x, y = y)) +
geom_violin()
}
for(column in colnames(happiness[,6:13])){
print(FUN(happiness, "region", column ))
}
FUN <- function(dat, x, y) {
ggplot(dat, aes_string(x = x, y = y)) +
geom_violin() +
theme(axis.text.x=element_text(angle=90, hjust=1))
}
for(column in colnames(happiness[,6:13])){
print(FUN(happiness, "region", column ))
}
FUN <- function(dat, x, y) {
ggplot(dat, aes_string(x = x, y = y)) +
geom_violin() +
theme(axis.text.x=element_text(angle=40, hjust=0.5))
}
for(column in colnames(happiness[,6:13])){
print(FUN(happiness, "region", column ))
}
FUN <- function(dat, x, y) {
ggplot(dat, aes_string(x = x, y = y)) +
geom_violin() +
coord_flip()
}
for(column in colnames(happiness[,6:13])){
print(FUN(happiness, "region", column ))
}
FUN <- function(dat, x, y) {
ggplot(dat, aes_string(x = x, y = y)) +
geom_boxplot() +
coord_flip()
}
for(column in colnames(happiness[,6:13])){
print(FUN(happiness, "region", column ))
}
FUN <- function(dat, x, y) {
ggplot(dat, aes_string(x = x, y = y)) +
geom_boxplot() +
coord_flip()
}
for(column in colnames(happiness[,6:13])){
print(FUN(happiness, "region", column ))
}
for(column in colnames(happiness[,6:13])){
print(FUN(happiness, "year", column ))
}
FIN.